package com.ttn.auth.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.auth.service.SysUserService;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.auth.SysUser;
import com.ttn.common.core.entity.auth.query.SysUserPageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * (sys_user)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/sys-user")
@Slf4j
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 新建系统用户
     *
     * @param request
     * @param sysUser
     * @return
     */

    @PostMapping("/save/sys-user")
    public Result saveSysUser(HttpServletRequest request, @RequestBody SysUser sysUser) {
        log.info("SysUserController saveSysUser ,参数:{}", sysUser);
        sysUserService.save(sysUser);
        return Result.success();
    }

    /**
     * 修改用户和权限
     *
     * @param request
     * @param sysUser
     * @return
     */

    @PutMapping("/modify/sys-user")
    public Result modifySysUser(HttpServletRequest request, @RequestBody SysUser sysUser) {
        log.info("SysUserController modifySysUser ,参数:{}", sysUser);
        sysUserService.updateById(sysUser);
        return Result.success();
    }

    /**
     * 重置密码
     *
     * @param request
     * @param sysUserId
     * @return
     */
    @PostMapping("/modify/reset-password/{sysUserId}")
    public Result modifyResetPassword(HttpServletRequest request, @PathVariable("sysUserId") int sysUserId) {
        log.info("SysUserController modifyResetPassword ,参数:{}", sysUserId);
        SysUser sysUser = sysUserService.getById(sysUserId);
        sysUser.setPassword("123456");
        sysUserService.updateById(sysUser);
        return Result.success();
    }

    /**
     * 分页查询系统用户信息
     *
     * @param request
     * @param sysUserPageQuery
     * @return
     */

    @PostMapping("/query/sys-user-page")
    public Result querySysUserPage(HttpServletRequest request, @RequestBody SysUserPageQuery sysUserPageQuery) {
        log.info("SysUserController sysUserPageQuery ,参数:{}", sysUserPageQuery);
        IPage<SysUser> sysUserIPage = new Page<>(sysUserPageQuery.getPageNum(), sysUserPageQuery.getPageSize());
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(StrUtil.isNotBlank(sysUserPageQuery.getUserName()), SysUser::getUserName, sysUserPageQuery.getUserName());
        sysUserLambdaQueryWrapper.eq(StrUtil.isNotBlank(sysUserPageQuery.getNickName()), SysUser::getNickName, sysUserPageQuery.getNickName());
        sysUserLambdaQueryWrapper.eq(StrUtil.isNotBlank(sysUserPageQuery.getMobile()), SysUser::getUserName, sysUserPageQuery.getUserName());
        sysUserLambdaQueryWrapper.eq(Objects.nonNull(sysUserPageQuery.getStatus()), SysUser::getStatus, sysUserPageQuery.getStatus());
        return Result.success(sysUserService.page(sysUserIPage, sysUserLambdaQueryWrapper));
    }

    /**
     * 通过系统id,获取系统用户详情
     *
     * @param request
     * @param sysUserId
     * @return
     */
    @PostMapping("/query/sys-user-details/{sysUserId}")
    public Result querySysUserDetails(HttpServletRequest request, @PathVariable("sysUserId") int sysUserId) {
        log.info("SysUserController querySysUserDetails ,参数:{}", sysUserId);

        return sysUserService.querySysUserDetails(sysUserId);
    }


}
